#!/bin/bash

CUR_DIR=$(cd $(dirname ${BASH_SOURCE[0]}); pwd)
TOP_DIR=$(cd $CUR_DIR/..; pwd)
SRS_DIR=$(cd $TOP_DIR/projects/srs4.0; pwd)

cd $SRS_DIR

DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
VERSION=$(git describe --tags --always)
URL=$(git config --get remote.origin.url)
COMMIT=$(git rev-parse HEAD | cut -c 1-7)
BRANCH=$(git rev-parse --abbrev-ref HEAD)
NUMBER=$(git rev-list HEAD | wc -l | awk '{print $1}')

echo "DATE: $DATE"
echo "VERSION: $VERSION"
echo "URL: $URL"
echo "COMMIT: $COMMIT"
echo "BRANCH: $BRANCH"
echo "NUMBER: $NUMBER"

VENDOR=ossrs
PROJECT=srs
TAG="${VERSION}_${COMMIT}"
REPOSITORY="${VENDOR}/${PROJECT}"

cd $TOP_DIR
docker build --tag $REPOSITORY:$TAG \
    --build-arg VENDOR=$VENDOR \
    --build-arg PROJECT=$PROJECT \
    --build-arg REPOSITORY=$REPOSITORY \
    --build-arg TAG=$TAG \
    --build-arg DATE=$DATE \
    --build-arg VERSION=$VERSION \
    --build-arg URL=$URL \
    --build-arg COMMIT=$COMMIT \
    --build-arg BRANCH=$BRANCH \
    --file Dockerfile.srs .

docker tag $REPOSITORY:$TAG $REPOSITORY:v4.0
